<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>es6 继承</title>
</head>
<body>
    <script>
        // 定义一个父类
        class Animal {
            constructor(name) {
                this.name = name;
            }
            speak() {
                console.log(`${this.name} makes a noise.`);
            }
        }

        // 定义一个子类，继承自父类
        class Dog extends Animal {
            constructor(name, age) {
                // 调用父类的构造函数
                super(name);
                this.age = age;
            }
            speak() {
                super.speak(); // 调用父类的方法
                console.log(`${this.name} barks. ${this.age} years old.`);
            }
        }

        // 创建一个子类的实例
        const dog = new Dog('Rex', 20);
        dog.speak(); // 输出: Rex barks.
    </script>
</body>
</html>